package com.bigflexlib.x.air.update
{
	import air.update.ApplicationUpdaterUI;
	import air.update.events.DownloadErrorEvent;
	import air.update.events.UpdateEvent;
	
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.filesystem.File;
	
	import mx.controls.Alert;

	public class UpdateHelper
	{

		private static var updater:ApplicationUpdaterUI = new ApplicationUpdaterUI();
		
		/** 
		 * <p>Checks for application updates, using the update configuration file
		 * pointed by the path parameter.</p>
		 *
         * <p>In case of error, this just shows an Alert to the user. If more
         * control over failure handling is needed, use the air update API
         * directly.</p>
         *
         * <p> Example code:
         *
         * <pre>
         *     UNIVRZUtil.checkForUpdates("app:/updateConfig.xml");
         * </pre>
         * </p>
         */
		public static function checkForUpdates(configurationPath:String):void {
			// Note that updater is an static variable
			// TODO: refactor this to x.mx... package
			updater.configurationFile = new File(configurationPath);
			updater.isCheckForUpdateVisible = false;
			updater.addEventListener(UpdateEvent.INITIALIZED, function(e:Event):void
			{
				updater.checkNow();
			});
			updater.addEventListener(ErrorEvent.ERROR, function(e:ErrorEvent):void
			{
				Alert.show("Update Error: " + e.toString());
			});
			updater.addEventListener(DownloadErrorEvent.DOWNLOAD_ERROR, function(e:DownloadErrorEvent):void
			{
				Alert.show("Update Download Error: " + e.toString());
			});
			updater.initialize();
		}
	}
}